using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Siding : MonoBehaviour
{
	[SerializeField]
	private RectTransform m_SideballO;

	[SerializeField]
	private RectTransform m_AnchorO;

	[SerializeField]
	private SidingAnchor m_SidingAnchor;

	[SerializeField]
	private Text m_MasseText;

	[SerializeField]
	private RectTransform m_MasseCue;

	[SerializeField]
	private RectTransform m_MasseIcon;

	private Vector2 m_LastCoord;

	private const float m_FadeTime = 0.5f;

	public Transform anchorO => m_AnchorO.transform;

	public RectTransform sideBallO => m_SideballO;

	public Transform masseText => m_MasseText.transform;

	public int MasseAngle
	{
		get
		{
			Vector3 localEulerAngles = m_MasseCue.localEulerAngles;
			return Mathf.RoundToInt(localEulerAngles.z);
		}
		set
		{
			m_MasseCue.localEulerAngles = new Vector3(0f, 0f, value);
			m_MasseText.text = value.ToString() + "°";
			m_MasseText.transform.rotation = Quaternion.identity;
			Pools.Cue.VerticalRotate(value);
			iTween.RotateTo(m_MasseIcon.gameObject, m_MasseCue.localEulerAngles, 0.2f);
		}
	}

	public Vector2 sidingOffset => m_SidingAnchor.anchorOffset;

	public SidingAnchor sidingAnchor => m_SidingAnchor;

	private void Awake()
	{
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Combine(PoolRulesBase.onNewTurn, new Delegate1Args<int>(ResetAnchorOffset));
		m_MasseCue.localEulerAngles = Vector3.zero;
		m_MasseIcon.localEulerAngles = Vector3.zero;
	}

	private void OnDestroy()
	{
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Remove(PoolRulesBase.onNewTurn, new Delegate1Args<int>(ResetAnchorOffset));
	}

	public void CalculateAnchorOffset(Vector3 vc)
	{
		Vector2 vector = m_SideballO.worldToLocalMatrix.MultiplyPoint(vc);
		float magnitude = vector.magnitude;
		float num = m_SideballO.rect.width * 0.5f - m_AnchorO.rect.width * 0.5f;
		Vector2 offset = default(Vector2);
		if (magnitude > num)
		{
			float f = Mathf.Acos(Mathf.Abs(vector.x) / magnitude);
			offset.x = num * Mathf.Cos(f) * (float)((vector.x >= 0f) ? 1 : (-1));
			offset.y = num * Mathf.Sin(f) * (float)((vector.y >= 0f) ? 1 : (-1));
		}
		else
		{
			offset = vector;
		}
		_SidingAnchorOffset(offset);
	}

	public void CalculateMasseCueOffset(Vector3 worldPosition)
	{
		Vector3 normalized = (worldPosition - m_MasseCue.position).normalized;
		Vector3 vector = Vector3.Cross(normalized, Vector3.right);
		int value = (!(vector.z > 0f)) ? ((int)Vector3.Angle(Vector3.right, normalized)) : 0;
		value = (MasseAngle = Mathf.Clamp(value, 0, 90));
	}

	public void OnSiding(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		Vector2 v = BaseUIController.GetUICamera().ScreenToWorldPoint(pointerEventData.position);
		CalculateAnchorOffset(v);
	}

	private void _SidingAnchorOffset(Vector2 offset)
	{
		m_SidingAnchor.SetTargetPosition(offset);
	}

	private void _ResetVerticalDegreeSlider()
	{
		m_MasseCue.localEulerAngles = Vector3.zero;
	}

	private void ResetAnchorOffset(int i)
	{
		_SidingAnchorOffset(Vector2.zero);
		_ResetVerticalDegreeSlider();
		MasseAngle = 0;
		Pools.Cue.Siding(Vector3.zero);
	}

	public void OnCueDrag(BaseEventData data)
	{
		PointerEventData pointerEventData = data as PointerEventData;
		Vector3 worldPosition = BaseUIController.GetUICamera().ScreenToWorldPoint(pointerEventData.position);
		CalculateMasseCueOffset(worldPosition);
	}
}
